package com.snail.jj.androiddemos.bases;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.view.View;
import android.widget.TextView;

import com.snail.jj.androiddemos.R;

/**
 * Created by snail on 2017/4/11.
 */

public class NavigationBar extends RelativeLayout implements View.OnClickListener {


    private ImageView leftImageView;
    private TextView titleTextView;
    private TextView rightTextView;

    BaseActivity mContext;


    public NavigationBar(Context context) {
        this(context,null);
    }

    public NavigationBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = (BaseActivity)getContext();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContext = (BaseActivity)getContext();
        leftImageView = (ImageView) findViewById(R.id.base_leftImage);
        titleTextView = (TextView) findViewById(R.id.base_title);
        rightTextView = (TextView) findViewById(R.id.base_rightText);
        leftImageView.setOnClickListener(this);
        rightTextView.setOnClickListener(this);
        titleTextView.setOnClickListener(this);
        titleTextView.setClickable(false);
    }

    /**
     * 右边控件是否可见
     */
    public void setRightViewVisiable(int visiable) {
        rightTextView.setVisibility(visiable);
    }


    /**
     * 右边控件的文字
     */
    public void setRightText(String tTitle) {
        rightTextView.setText(tTitle);
    }

    /**
     * 返回右边控件
     */
    public TextView getRightTextView() {
        return rightTextView;
    }

    /**
     * 中间控件的点击事件
     */
    public void setTitleClick(boolean canClick) {
        titleTextView.setClickable(canClick);
    }

    /**
     * 中间控件文本
     */
    public void setTitle(String title) {
        titleTextView.setText(title);
    }

    public void setLeftViewVisiable(int visiable) {
        leftImageView.setVisibility(visiable);
    }

    public void setLeftImage(Drawable drawable) {
        leftImageView.setImageDrawable(drawable);
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_leftImage:
                mContext.leftViewAction();
                break;
            case R.id.base_title:
                Log.i("ada","title");
                break;
            case R.id.base_rightText:
                mContext.rightViewAction();
                break;
            default:
                break;
        }
    }
}
